package com.yuwj.chinaa.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.entity.EntityAppInfo;
import com.yuwj.chinaa.entity.EntityBurseCoinNumber;
import com.yuwj.chinaa.entity.EntityUser;

/**
 * sharedpreference帮助类
 */
public class SharedpreferenceUtil {

    private static SharedpreferenceUtil instance = null;

    private SharedpreferenceUtil() {
    }

    public static synchronized SharedpreferenceUtil getInstance() {
        if (instance == null) {
            instance = new SharedpreferenceUtil();
        }
        return instance;
    }

    /**
     * 保存用户名
     *
     * @param context
     * @param username
     */
    public void saveUsername(Context context, String username) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("username", username);
        editor.commit();
    }

    /**
     * 获取用户名
     *
     * @param context
     * @return
     */
    public String getUsername(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        String username = preferences.getString("username", "");
        return username;
    }


    /**
     * 保存用户信息到配置文件
     *
     * @param context x
     * @param user    x
     */
    public void saveUserInfo(Context context, EntityUser user) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("entityUser", GsonUtil.bean2Json(user));
        editor.commit();
    }


    /**
     * 清除用户信息
     *
     * @param context x
     */
    public void clearUserInfo(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.remove("entityUser");
        editor.commit();
    }


    /**
     * 功能描述：读取本地用户信息设置
     *
     * @param context x
     */
    public EntityUser getUserInfo(Context context) {
        EntityUser entityUser = null;
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        String userJsonStr = preferences.getString("entityUser", "");
        if (!TextUtils.isEmpty(userJsonStr)) {
            entityUser = GsonUtil.json2Bean(userJsonStr, EntityUser.class);
        }
        return entityUser;
    }

    /**
     * 保存用户各种资产
     *
     * @param context
     * @param entityBurseCoinNumber
     */
    public void saveUserBurseCoinNumber(Context context, EntityBurseCoinNumber entityBurseCoinNumber) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("entityBurseCoinNumber", GsonUtil.bean2Json(entityBurseCoinNumber));
        editor.commit();
    }

    /**
     * 获取用户各种资产
     *
     * @param context
     * @return
     */
    public EntityBurseCoinNumber getUserBurseCoinNumber(Context context) {
        EntityBurseCoinNumber entityBurseCoinNumber = null;
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        String entityBurseCoinNumberJsonStr = preferences.getString("entityBurseCoinNumber", "");
        if (!TextUtils.isEmpty(entityBurseCoinNumberJsonStr)) {
            entityBurseCoinNumber = GsonUtil.json2Bean(entityBurseCoinNumberJsonStr, EntityBurseCoinNumber.class);
        }
        return entityBurseCoinNumber;
    }

    /**
     * 保存是否是首次加载APP
     *
     * @param context
     * @param isFirstLoadApp
     */
    public void saveIsFirstLoadApp(Context context, boolean isFirstLoadApp) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putBoolean("isFirstLoadApp", isFirstLoadApp);
        editor.commit();
    }

    /**
     * 是否是首次加载APP
     *
     * @param context
     * @return
     */
    public boolean getIsFirstLoadApp(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        return preferences.getBoolean("isFirstLoadApp", true);
    }


    /**
     * 保存是否是首次加载APP
     *
     * @param context
     */
    public void saveTodayPrice(Context context, float price) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putFloat("todayPrice", price);
        editor.commit();
    }

    /**
     * 是否是首次加载APP
     *
     * @param context
     * @return
     */
    public float getTodayPrice(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        return preferences.getFloat("todayPrice", 0);
    }

    /**
     * 保存更新信息
     *
     * @param context
     * @param entityAppInfo
     */
    public void saveUpdateInfo(Context context, EntityAppInfo entityAppInfo) {
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("entityAppInfo", GsonUtil.bean2Json(entityAppInfo));
        editor.commit();
    }

    /**
     * 获取更新信息
     *
     * @param context
     * @return
     */
    public EntityAppInfo getEntityAppInfo(Context context) {
        EntityAppInfo entityAppInfo = null;
        SharedPreferences preferences = context.getSharedPreferences(
                ConstantParam.SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        String str = preferences.getString("entityAppInfo", "");
        if (!TextUtils.isEmpty(str)) {
            entityAppInfo = GsonUtil.json2Bean(str, EntityAppInfo.class);
        }
        return entityAppInfo;
    }
}
